<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="key" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search-submit">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加预选 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deletePart"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
            <a class="layui-btn layui-btn-xs " lay-event="distribute" style="background: #2ecc71">分配宿舍</a>
        </script>
    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','miniAdmin','form','element','axios'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            axios = layui.axios,
            miniAdmin = layui.miniAdmin,
            miniPage = layui.miniPage;
      
        table.render({
            elem: '#currentTableId',
            method:'post',
            url: 'selection/getList',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {type: 'numbers', width: 80, title: 'ID'},
                {field: 'name',title: '名称'},
                {field: 'gradeId', title: '年级'},
                {field: 'className', title: '班级'},
                {field: 'startTime', title: '开始时间'},
                {field: 'endTime', title: '结束时间'},
                {field: 'remark',title: '备注'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [5, 10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "data": res.data.selections, //解析数据长度
                    "count": res.data.count //解析数据列表
                };
            }
        });

        window.reload = function(){
            table.reload('currentTableId');
        }

        // 监听搜索操作
        form.on('submit(search-submit)', function (data) {

            table.reload('currentTableId', {
                where: data.field
            });
            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                let content = miniPage.getHrefContent('selection/add.html');
                let openWH = miniPage.getOpenWidthHeight();
                let index = layer.open({
                    title: '添加预选',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'deletePart') {  //批量删除
                let data = table.checkStatus('currentTableId').data
                if(data.length==0){
                  layer.msg("请至少选择一条数据删除",{
                              icon:5,
                              time:2000,
                              anim:6
                            })       
                }else{
                    layer.confirm('真的要删除这些预选数据吗？', function (index) {
                        let selectionIds = data.map(item=>item.id)
                        selectionIdsStr = selectionIds.join(",")
                        axios.delete("selection/deletePart/"+selectionIdsStr)
                            .then(data=>{
                                layer.msg(data.message,{
                                  icon:6,
                                  time:2000
                                },function () {
                                    table.reload("currentTableId")
                                })
                            }).catch(error=>{
                            layer.msg("请求错误",{
                              icon:5,
                              time:2000,
                              anim:6
                            })
                        });
                    });
                }
            }
        });

        function setFormValue(data){
            form.val("editForm",{
                id:data.id,
                name:data.name,
                startTime:data.startTime,
                endTime:data.endTime,
                remark:data.remark,
                classId:data.classId,
                gradeId:data.gradeId,
                className:data.className,
            })
        }

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;//一行数据
            if (obj.event === 'edit') {
                let content = miniPage.getHrefContent('selection/edit.html');
                let openWH = miniPage.getOpenWidthHeight();
                let index = layer.open({
                    title: '编辑预选',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content
                });
                axios({
                    method:"get",
                    url:"grade/list"
                }).then(response=> {
                    response_data = response.data.grades
                    let options = '<option value="">请选择年级</option>';
                    for (let obj of response_data) {
                        options += '<option value="' + obj.id + '">' + obj.name + '</option>'
                    }
                    $('#gradeId').append(options);
                    form.render('select');//处理动态赋值
                    setFormValue(data);
                })
                

                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的要删除该预选数据吗？', function (index) {
                    axios({
                        method:'delete',
                        url:'/selection/deleteOne/'+data.id
                    }).then(data=>{
                        layer.msg(data.message,{
                          icon:6,
                          time:2000
                        },function () {
                            table.reload("currentTableId")
                        })
                    }).catch(error=>{
                        layer.msg("请求错误",{
                          icon:5,
                          time:2000,
                          anim:6
                        })
                    });
                    layer.close(index);
                });
            }else if (obj.event === 'distribute') {  //分配操作
                //数据的保存 需要放在页面打开之前
                window.seletionData = function(){
                  return data;
                }
                let content = miniPage.getHrefContent('selection/distribute.html');
                let openWH = miniPage.getOpenWidthHeight();
                let index = layer.open({
                    title: '分配宿舍',
                    type: 1,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
        });

    });
</script>